//
//  KYSettingViewController.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/9/21.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYSettingViewController.h"
#import "KYSensitivityController.h"
#import "KYFeedBackController.h"
#import "KYAboutController.h"
#import "KYCustomShareView.h"

@interface KYSettingViewController ()<KYCustomShareViewDelegate>

@end

@implementation KYSettingViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 0.设置标题
    self.title = NSLocalizedString(@"设置", nil);
    
    // 1.添加分组
    [self setUpGroup0];
    [self setUpGroup1];
    [self setUpGroup2];
}

- (void)setUpGroup0
{
    KYSettingGroup *group = [self addGroup];
    
    KYSettingSwitchItem *shake = [KYSettingSwitchItem itemWithTitle:NSLocalizedString(@"震动", nil)];
    
    KYSettingSwitchItem *voice = [KYSettingSwitchItem itemWithTitle:NSLocalizedString(@"声音", nil)];
    
    KYSettingLabelItem *sensitivity = [KYSettingLabelItem itemWithTitle:NSLocalizedString(@"灵敏度", nil) destVcClass:[KYSensitivityController class]];
    sensitivity.defaultText = NSLocalizedString(@"中", nil);
    sensitivity.readyForDestVc = ^(KYSettingLabelItem *item,KYSensitivityController *destVc){
        destVc.sourceItem = item;
    };
    
    group.items = @[shake,voice,sensitivity];
    group.header = NSLocalizedString(@"摇一摇设置", nil);
}

- (void)setUpGroup1
{
    __weak typeof(self) selfVc = self;
    
    KYSettingGroup *group = [self addGroup];
    
    KYSettingSwitchItem *notice = [KYSettingSwitchItem itemWithTitle:NSLocalizedString(@"消息通知", nil)];
    
    __weak KYSettingItem *dataCache = [KYSettingItem itemWithTitle:NSLocalizedString(@"数据和缓存", nil)];
    __block NSInteger temp = arc4random()%10;
    dataCache.detailLabel = [NSString stringWithFormat:@"%ldM",temp];
    
    dataCache.option = ^{
        
        if (temp == 0) return ;
        [MBProgressHUD showMessage:NSLocalizedString(@"正在清理缓存数据...", nil)];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            
            [MBProgressHUD showSuccess:[NSString stringWithFormat:@"清理%ldM缓存...",temp]];
            
            dataCache.detailLabel = NSLocalizedString(@"0M", nil);
            
            temp = 0;
            
            [selfVc.tableView reloadData];
        });
    };

    KYSettingArrowItem *collect = [KYSettingArrowItem itemWithTitle:NSLocalizedString(@"收藏", nil) destVcClass:nil];
    
    KYSettingArrowItem *history = [KYSettingArrowItem itemWithTitle:NSLocalizedString(@"历史记录", nil) destVcClass:nil];
    
    KYSettingItem *shareFriend = [KYSettingItem itemWithTitle:NSLocalizedString(@"分享好友", nil)];
    shareFriend.option = ^{
        KYCustomShareView *shareView = [KYCustomShareView customShareView];
        shareView.delegate = selfVc;
        shareView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        [shareView show];
    };
    
    group.items = @[notice,dataCache,collect,history,shareFriend];
    group.header = NSLocalizedString(@"通用设置", nil);
}

- (void)setUpGroup2
{
    KYSettingGroup *group = [self addGroup];
    
#warning - NO发送请求对比版本号
    KYSettingArrowItem *updates = [KYSettingArrowItem itemWithTitle:NSLocalizedString(@"检查更新", nil) destVcClass:nil];
    updates.option = ^{
        [MBProgressHUD showMessage:NSLocalizedString(@"正在检测最新版本...", nil)];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            
            [MBProgressHUD showSuccess:NSLocalizedString(@"当前为最新版本...", nil)];
        
        });
    };
    
    KYSettingArrowItem *msgBack = [KYSettingArrowItem itemWithTitle:NSLocalizedString(@"意见反馈", nil) destVcClass:[KYFeedBackController class]];
    
    KYSettingArrowItem *about = [KYSettingArrowItem itemWithTitle:NSLocalizedString(@"关于", nil) destVcClass:[KYAboutController class]];
    
    group.items = @[updates,msgBack,about];
    group.header = NSLocalizedString(@"帮助与反馈", nil);
}
@end
